package com.wan37.client.message.received;

import com.google.protobuf.Message;
import com.googlecode.protobuf.format.JsonFormat;
import com.wan37.client.service.cache.TeamCache;
import com.wan37.protobuf.constant.CommonTeam;
import com.wan37.protobuf.constant.PtoNum;
import com.wan37.protobuf.message.client2server.game.GameTeam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author : luoyong
 * @date : 2020-07-30 18:32
 **/
@Component(PtoNum.TEAMRESPONSE + "")
public class TeamResponse implements ReceiveMsg{
    private final Logger logger = LoggerFactory.getLogger(TeamResponse.class);
    @Autowired
    private TeamCache teamCache;
    @Override
    public void execute(Message msg) {
        GameTeam.TeamResponse teamResponse = (GameTeam.TeamResponse) msg;
        int code = teamResponse.getCode();
        switch (code){
            case CommonTeam.CREATE:
                GameTeam.Team team = teamResponse.getTeamList().get(0);
                TeamCache.TeamIn teamIn = teamCache.new TeamIn();
                teamIn.setTeam(team);
                teamCache.getTeamInMap().put(team.getTeamName(), teamIn);
                logger.info("队伍创建成功！详细情况：{}", JsonFormat.printToString(team));
                break;
            case CommonTeam.GET_ALL:
                logger.info("全部队伍详情：");
                for (GameTeam.Team team1 : teamResponse.getTeamList()){
                    logger.info(JsonFormat.printToString(team1));
                }
                break;
            case CommonTeam.UPDATE:
                GameTeam.Team team2 = teamResponse.getTeamList().get(0);
                TeamCache.TeamIn teamIn2 = teamCache.new TeamIn();
                teamIn2.setTeam(team2);
                teamCache.getTeamInMap().put(team2.getTeamName(), teamIn2);
                logger.info("队伍详细情况：{}", JsonFormat.printToString(team2));
                break;
            case CommonTeam.OUT:
                logger.info("成功离开队伍！");
                break;
            case CommonTeam.EXIST_TEAM_IN:
                logger.info("你已经在一个队伍中！");
                break;
            case CommonTeam.EXIST:
                logger.info("你想创建的队伍，队伍名已经存在！");
                break;
            case CommonTeam.NOT_EXIST:
                logger.info("你输入的队伍并不存在！");
                break;
            case CommonTeam.NOT_TEAM_IN:
                logger.info("你并不在队伍中！");
                break;

            default:
                break;
        }
    }
}
